<?php

    class QEditableLabelToolbar extends QPanel {
        
        public function __construct($objParentObject, $strControlId = null) {
            parent::__construct($objParentObject, $strControlId);
            
            $this->Template = '../../admin/controls/QEditableLabelToolbar.tpl.php';
        }
        
    }

	class QEditableLabel extends QTextBox {
            
            protected $blnEditing = false;
            protected $strBackup;
            
            public $btnEdit;
            public $btnSave;
            public $btnCancel;
            public $objWaitIcon;
            public $objToolbar;
            
            public function __construct($objParentObject, $strControlId = null) {
                    parent::__construct($objParentObject, $strControlId);
                    
                    $this->AddCssFile('../../../admin/content/plugins/font-awesome/css/font-awesome.min.css');

                    //$this->AddJavascriptFile('../../../admin/content/plugins/bootstrap/js/bootstrap.min.js');
                    $this->AddJavascriptFile('../../../admin/content/plugins/bootstrap-wysihtml5/parser_rules/custom.js');
                    $this->AddJavascriptFile('../../../admin/content/plugins/bootstrap-wysihtml5/lib/js/wysihtml5-0.3.0.min.js');
                    //$this->AddJavascriptFile('../../../admin/content/plugins/bootstrap-wysihtml5/dist/bootstrap-wysihtml5-0.0.2.min.js');
                    
                    $this->objToolbar = new QEditableLabelToolbar($this);
                    
                    $this->objWaitIcon = new QWaitIcon($this);
                    $this->objWaitIcon->Text = '<i class="icon-cog icon-spin icon-large"></i>';
                    
                    $this->btnEdit = new QLinkButton($this);
                    $this->btnEdit->Text = '[Edit]';
                    $this->btnEdit->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnEdit_Click', $this->objWaitIcon));
                    $this->btnEdit->AddAction(new QClickEvent(), new QTerminateAction());
                    
                    $this->btnSave = new QLinkButton($this);
                    $this->btnSave->Text = '[Save]';
                    $this->btnSave->CausesValidation = true;
                    $this->btnSave->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSave_Click', $this->objWaitIcon));
                    $this->btnSave->AddAction(new QClickEvent(), new QTerminateAction());
                    
                    $this->btnCancel = new QLinkButton($this);
                    $this->btnCancel->Text = '[Cancel]';
                    $this->btnCancel->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnCancel_Click', $this->objWaitIcon));
                    $this->btnCancel->AddAction(new QClickEvent(), new QTerminateAction());
            }
            
            protected function GetControlHtml() {
                    
                        switch (true) {
                            
                                case ($this->TextMode == QTextMode::MultiLine && strpos($this->CssClass, 'wysihtml5') !== FALSE && $this->blnEditing === TRUE):
                                        return sprintf('%s%s%s%s%s',
                                                    $this->objToolbar->Render(false),
                                                    parent::GetControlHtml(),
                                                    $this->objWaitIcon->Render(false),
                                                    $this->btnSave->Render(false),
                                                    $this->btnCancel->Render(false));
                                
                                case $this->blnEditing:
                                        return sprintf('%s%s%s%s',
                                                    parent::GetControlHtml(),
                                                    $this->objWaitIcon->Render(false),
                                                    $this->btnSave->Render(false),
                                                    $this->btnCancel->Render(false));
                                
                                case $this->blnReadOnly:
                                        return $this->strText;
				
                                default:
                                        return sprintf('%s%s%s',
                                                    $this->strText,
                                                    $this->objWaitIcon->Render(false),
                                                    $this->btnEdit->Render(false));
			
                        }
            }
            
            protected function GetControlJavaScript() {
                //return sprintf('jQuery("#%s").wysihtml5({%s})', $this->ControlId, $this->makeJqOptions());
                
                return sprintf('new wysihtml5.Editor("%s",({%s}))', $this->ControlId, $this->makeJqOptions());
            }
            
            public function GetEndScript() {
                if ($this->TextMode == QTextMode::MultiLine && strpos($this->CssClass, 'wysihtml5') !== FALSE && $this->blnEditing === TRUE) {
                    return  $this->GetControlJavaScript() . '; ' . parent::GetEndScript();
                }
                
                return parent::GetEndScript();
            }            
            
            public function btnEdit_Click($strFormId, $strControlId, $strParameter) {
                
                $this->blnEditing = true;
                $this->strBackup = $this->Text;
                
                $this->Refresh();
            }
            
            public function btnSave_Click($strFormId, $strControlId, $strParameter) {
                
                $objContentKey = ContentKey::Load($this->ActionParameter);
                $objContentKey->Value = $this->Text;
                $objContentKey->Save();
                
                $this->blnEditing = false;
                $this->strBackup = $this->Text;
                
                $this->Refresh();
            }
            
            public function btnCancel_Click($strFormId, $strControlId, $strParameter) {
                
                $this->blnEditing = false;
                $this->Text = $this->strBackup;
                
                $this->Refresh();
            }
            
            private function makeJsProperty($strProp, $strKey) {
                
                $objValue = $strProp;
                if (null === $objValue) {
                    return '';
                }

                return $strKey . ': ' . JavaScriptHelper::toJsObject($objValue) . ', ';
            }

            private function makeJqOptions() {
                
                $strJqOptions = '';
                $strJqOptions .= $this->makeJsProperty($this->objToolbar->ControlId, 'toolbar');
                $strJqOptions .= 'parserRules: wysihtml5ParserRules, ';
                //$strJqOptions .= $this->makeJsProperty(true, 'cleanUp');
                //$strJqOptions .= $this->makeJsProperty('./../../admin/content/plugins/bootstrap/css/skin/cosmo/bootstrap.min.css', 'stylesheets');
                
                //$strJqOptions .= $this->makeJsProperty(false, '\'font-styles\'');
                //$strJqOptions .= $this->makeJsProperty(true, 'html');
                //$strJqOptions .= $this->makeJsProperty(false, 'image');
                //$strJqOptions .= $this->makeJsProperty(true, 'cleanUp');
                //$strJqOptions .= $this->makeJsProperty('./../../admin/content/plugins/bootstrap/css/skin/cosmo/bootstrap.min.css', 'stylesheets');
                
                //$strJqOptions .= $this->makeJsProperty('OnBeforeShow', 'beforeShow');
                //$strJqOptions .= $this->makeJsProperty('OnBeforeShowDay', 'beforeShowDay');
                //$strJqOptions .= $this->makeJsProperty('OnChangeMonthYear', 'onChangeMonthYear');
                //$strJqOptions .= $this->makeJsProperty('OnClose', 'onClose');
                //$strJqOptions .= $this->makeJsProperty('OnSelect', 'onSelect');
                
                if ($strJqOptions) $strJqOptions = substr($strJqOptions, 0, -2);
                
                return $strJqOptions;
            }
            
            /*
             * 
             * function 
    
    var editor = $j('.wysihtml5').data('wysihtml5');
            
    if (!editor) {

        $j('.wysihtml5').wysihtml5({
            'font-styles': false, 
            html: true, 
            image: false,
            cleanUp: true,
            stylesheets: ["./../../content/plugins/bootstrap/css/skin/cosmo/bootstrap.min.css"]
        });
    }
    
             * 
             *              */
        }
?>